package pl.th.permission;

import android.content.Intent;
import android.os.Build;
import android.provider.Settings;

import static android.provider.Settings.EXTRA_APP_PACKAGE;

/**
 * request the permission to show Notification
 */
public class NotificationPerKProcessor extends AbstractProcessor{

    public NotificationPerKProcessor(String... permissions) {
        super(permissions);
    }

    @Override
    @SuppressWarnings({"ConstantConditions", "InlinedApi"})
    public void process(ProxyFragment proxy) {
        if (PermissionCompat.isNotificationPerGranted(proxy.getContext())) {
            proxy.workOnNextProcessor();
        } else {
            try {
                Intent intent;
                if (Utils.isAndroidOAndUp(Build.VERSION.SDK_INT)) {
                    intent = new Intent();
                    intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                    intent.putExtra(EXTRA_APP_PACKAGE, proxy.getActivity().getPackageName());
                } else if (Utils.isAndroidLAndUp(Build.VERSION.SDK_INT)) {
                    intent = new Intent();
                    intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                    intent.putExtra("app_package", proxy.getActivity().getPackageName());
                    intent.putExtra("app_uid", proxy.getActivity().getApplicationInfo().uid);
                } else {
                    intent = PermissionCompat.buildAppDetailIntent(proxy.getContext());
                }
                proxy.startActivityForResult(intent);
            } catch (Exception e) {
                proxy.startActivityForResult(PermissionCompat.buildAppDetailIntent(proxy.getContext()));
            }
        }
    }
}
